/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * AddscoreServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.sundy.core.Configuration;
import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.tools.ExcelProcess;
import com.sundy.tools.MultipartSupport;

public class AddscoreServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6739344586848150807L;

	public AddscoreServlet() {
	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(context, request, response);
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newFileName;
		MultipartSupport ms = new MultipartSupport(request);
		FileItem item = ms.getFile("score");
		if (item == null) {
			Utility.showInfor(request, response, "没有上传文件！");
			return;
		}
		InputStream is = item.getInputStream();
		byte[] buffer = new byte[is.available()];
		is.read(buffer);
		newFileName = Configuration.getProperty("/config/tmp-dir")
				+ File.separator + Utility.getDate("yyyy-MM-dd-hh-mm-ss") + "-" + Utility.getFileName(item.getName());
		FileOutputStream fos = new FileOutputStream(newFileName);
		fos.write(buffer);
		fos.flush();
		fos.close();
		is.close();
		item.delete();
		String name = ms.getParameter("scname");
		String subtype = ms.getParameter("subtype");
		int subjectType = Integer.valueOf(subtype);
		if (ExcelProcess.Process(context, newFileName, name, subjectType))
			response.sendRedirect(request.getContextPath());
	}
}
